<template>
  <div class="login">
    <div class="img">
      <!-- <el-image :src="`${realSrc}`" fit="cover" :style="`width:${realWidth};height:${realHeight};`"
        :preview-src-list="realSrcList">
      </el-image> -->
    </div>
    <div class="content">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">厂网调控监控系统</h3>
        <span class="subTitle">账号登录</span>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <div class="form-footer">
          <!-- <router-link class="link-type" :to="'/systemActive'">系统激活</router-link> -->
          <nu-button class="link-type" type="text" @click="systemActiveClick" disabled style="cursor: no-drop;">系统激活</nu-button>
          <div class="forgot">
            <span>忘记密码</span>
            <el-tooltip class="item" effect="light" content="若您忘记登陆密码，可联系管理员进行重置" placement="right" :visible-arrow="false"
              :offset="30">
              <!-- <i class="el-icon-question" style="font-size: 14px;"></i> -->
              <span class="icon-forgot"></span>
            </el-tooltip>
          </div>
        </div>
        <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> -->
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
      <!--  底部  -->
      <div class="el-login-footer">
        <span>Copyright © 北京慧能分享科技有限公司 All Rights Reserved.</span>
      </div>
    </div>
    <nu-dialog :title="systemActive.title" :visible.sync="systemActive.open" width="30%" center
      :close-on-click-modal="false" top="50vh" :modal-append-to-body="false" @close="systemFormClose">
      <el-form ref="systemActiveForm" :model="systemActiveForm" :rules="registerRules" class="systemActive-form">
        <el-form-item prop="serial">
          <el-input v-model="systemActiveForm.serial" type="text" auto-complete="off" placeholder="请输入序列号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="registration">
          <el-input v-model="systemActiveForm.registration" type="number" auto-complete="off" placeholder="请输入注册码"
            @keyup.enter.native="handleRegister">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>

        <el-form-item style="width:100%;">
          <el-button :loading="systemLoading" size="medium" type="primary" style="width:100%;"
            @click.native.prevent="handleRegister">
            <span v-if="!systemLoading">系 统 激 活</span>
            <span v-else>激 活 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </nu-dialog>
  </div>
</template>

<script>
import { getCodeImg, systemActive } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import nuButton from "@/components/base/button"
import nuDialog from "@/components/base/dialog"
export default {
  name: "Login",
  components: { nuButton, nuDialog },
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      systemActive: {
        open: false,
        title: "系统激活",
      },
      systemActiveForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        serial: [
          { required: true, trigger: "blur", message: "请输入序列号" },
          { min: 2, max: 20, message: '序列号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        registration: [
          { required: true, trigger: "blur", message: "请输入注册码" },
          { min: 5, max: 20, message: '注册码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ]
      },
      systemLoading: false
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    if (this.captchaEnabled) {
      this.getCode();
    }
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: "/" });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
    systemActiveClick() {
      this.systemActive.open = true
    },
    systemFormClose() {
      this.resetForm()
      this.systemActive.open = false
    },
    /**
     * 系统激活
     */
    handleRegister() {
      this.$refs.systemActiveForm.validate(valid => {
        if (valid) {
          this.systemLoading = true;
          systemActive(this.systemActiveForm).then(res => {
            if (res.code == 200) {
              this.$alert("<font color='red'>" + " 激活成功！</font>", '系统提示', {
                dangerouslyUseHTMLString: true,
                type: 'success'
              })
              this.systemFormClose()
            } else {
              this.systemLoading = false;
              this.$alert("<font color='red'>激活失败，请联系系统运维人员。 " + " </font>", '系统提示', {
                dangerouslyUseHTMLString: true,
                type: 'info'
              })
            }
          }).catch(() => {
            this.systemLoading = false;
            this.$alert("<font color='red'>激活失败，请联系系统运维人员。 " + " </font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'info'
            })
          })
        }
      });
    },
    resetForm() {
      this.systemActiveForm = {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      }
      this.$refs['systemActiveForm'].resetFields();
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" >
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

  .img {
    height: 100%;
    background-image: url("../assets/images/Group-1@4x.png");
    flex: 1.7;
    background-size: cover;
    background-position: top center;
  }

  .content {
    flex: 1;
    min-width: 500px;
    height: 100%;
    background: rgba(255, 255, 255, 1);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .subTitle {
    font-size: 14px;
    color: rgba(0, 1, 59, 1);
    font-weight: 400;
    margin-bottom: 30px;
    display: inline-block;
  }

  .title {
    // margin: 0px auto 30px auto;
    // text-align: center;
    font-size: 32px;
    font-weight: 500;
    letter-spacing: 0px;
    line-height: 56px;
    color: rgba(48, 55, 92, 1);
    // text-align: left;
    vertical-align: top;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    margin: 0 0 90px 0;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 33%;
    height: 38px;
    float: right;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 20px;
    // width: 100%;
    text-align: center;
    color: rgba(182, 186, 210, 1);
    font-size: 12px;
    letter-spacing: 1px;
  }

  .login-code-img {
    height: 38px;
  }

  .form-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px 0 20px 0;

    .link-type {
      display: inline-block;
      color: rgba(0, 1, 59, 1);
      font-size: 14px;
      font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
    }

    .forgot {
      display: flex;
      color: rgba(0, 1, 59, 1);
      font-size: 14px;
      align-items: center;

      >span {
        margin: 0 5px;
      }

      .icon-forgot {
        display: inline-block;
        width: 20px;
        height: 20px;
        background-image: url("../assets/images/forgot.png");
        background-size: 100% 100%;
      }
    }
  }

  .el-dialog {
    border-radius: 20px !important;
    top: 50%;
    transform: translate(0, -70%);

    .el-dialog__header {
      padding: 0 40px;
      height: 62px;
      line-height: 90px;
      background: #fff !important;
      color: #000 !important;
      border-radius: 20px 20px 0 0;

      .el-dialog__title {
        font-size: 16px;
        color: #000 !important;
      }

      .el-dialog__headerbtn .el-dialog__close {
        color: #000 !important;
      }
    }

    .el-dialog__body {
      height: 100%;
      padding: 40px 55px;
      border-radius: 0 0 20px 20px;
    }
  }

  .el-button--primary {
    background: rgba(72, 118, 255, 1) !important;
    border-color: rgba(72, 118, 255, 1) !important;
  }

  .el-input__inner:focus {
    border-color: rgba(72, 118, 255, 1);
  }
}
</style>
